package com.haofengsoft.scce.ui.mine;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.mine.MinePersonalModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.mine.UserPersonalController;
import com.haofengsoft.scce.db.bean.UserInfo;
import com.haofengsoft.scce.db.cache.UserCache;

public class MinePersonalActivity extends Activity {

	private TextView nicknameTv;// 昵称
	private TextView realnameTv;// 真实姓名
	private TextView telTv;// 电话号
	private TextView emailTv;// 邮箱
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mine_personal_information);
		ImageView backIv = (ImageView) findViewById(R.id.user_personal_infomation_title_back_iv);
		backIv.setOnClickListener(new BackClickListener());
		progressBar = (ProgressBar) findViewById(R.id.user_personal_trade_pull_to_refresh_progress_pb);
		nicknameTv = (TextView) findViewById(R.id.user_nickname_tv);
		realnameTv = (TextView) findViewById(R.id.user_realname_tv);
		telTv = (TextView) findViewById(R.id.user_tel_tv);
		emailTv = (TextView) findViewById(R.id.user_email_tv);
		TextView logoutTv = (TextView) findViewById(R.id.user_personal_logout_tv);
		logoutTv.setOnClickListener(new LogoutClickListener());

	}

	class LogoutClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			UserCache.getInstance().clearCache(MinePersonalActivity.this);
			finish();
		}

	}

	class BackClickListener implements OnClickListener {
		@Override
		public void onClick(View view) {
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		initVolleyData();
	}

	private void initVolleyData() {
		// 访问后台获取用户数据
		progressBar.setVisibility(View.VISIBLE);
		Map<String, Object> mapParam = new HashMap<String, Object>();
		UserInfo userInfo = UserCache.getInstance().getUser(
				MinePersonalActivity.this);
		mapParam.put("ID", userInfo.getId());
		mapParam.put("LogID", userInfo.getId());
		VolleyManage.getInstance().methodPost(MinePersonalActivity.this, null,
				ConstantConfig.GET_USERINFO_URL, mapParam,
				new GetUserPersonalVolleyReal());
	}

	class GetUserPersonalVolleyReal implements VolleyInterface {

		@Override
		public void gainData(VolleyBean bean) {
			progressBar.setVisibility(View.GONE);
			if (bean.isSuccess()) {
				MinePersonalModel userPersonalModel = UserPersonalController
						.getInstance(MinePersonalActivity.this)
						.gainUserPersonalInfo(bean.getContent());
				initView(userPersonalModel);
			}
		}

	}

	private void initView(MinePersonalModel userPersonalModel) {
		nicknameTv
				.setText(StringUtil.isBlank(userPersonalModel.getNickName()) ? "--"
						: userPersonalModel.getNickName());
		realnameTv
				.setText(StringUtil.isBlank(userPersonalModel.getRealName()) ? "--"
						: userPersonalModel.getRealName());
		telTv.setText(StringUtil.isBlank(userPersonalModel.getTel()) ? "--"
				: userPersonalModel.getTel());
		emailTv.setText(StringUtil.isBlank(userPersonalModel.getEmail()) ? "--"
				: userPersonalModel.getEmail());

	}

}
